package com.theaf.core.rmq.conf;

import org.springframework.amqp.rabbit.connection.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.Properties;

/**
 * rabbitmq工厂配置
 */
@Configuration
public class RabbitMQConf {
    @Value("${rabbitmq.ip}")
    private String ip;
    @Value("${rabbitmq.port}")
    private String port;
    @Value("${rabbitmq.userName}")
    private String userName;
    @Value("${rabbitmq.password}")
    private String password;

    @Bean
    ConnectionFactory connectionFactory() {
        Properties properties = new Properties();

        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(ip, Integer.parseInt(port));

        connectionFactory.setUsername(userName);
        connectionFactory.setPassword(password);
        connectionFactory.setPublisherConfirms(true); // enable confirm mode
        connectionFactory.setChannelCacheSize(100);
        //connectionFactory.getRabbitConnectionFactory().setAutomaticRecoveryEnabled(true);

        return connectionFactory;
    }
}
